package licode.unisop.single.user.infrastructure.repository.dao;

import licode.unisop.realm.client.info.SiPlatformAssignIn;
import licode.unisop.realm.client.info.SiRealmGrantPlatformIn;
import licode.unisop.single.user.application.gateway.RealmPlatformGrantDao;
import licode.unisop.single.user.application.entity.RealmPlatformGrantEntity;
import licode.unisop.single.user.infrastructure.repository.po.RealmPlatformGrantPO;
import licode.unisop.single.user.infrastructure.repository.mapper.RealmPlatformGrantMapper;
import licode.unisop.single.user.infrastructure.assembler.UniUserSingleAssembler;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import licode.unisop.types.util.IdGenUtil;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * uni-user-single
 *
 * @author licode
 * @date 2024-06-29 10:19:05
 */
@Component
public class RealmPlatformGrantDaoImpl
        extends ServiceImpl<RealmPlatformGrantMapper, RealmPlatformGrantPO>
        implements RealmPlatformGrantDao {
    @Resource
    private UniUserSingleAssembler uniUserSingleAssembler;

    @Override
    public void assignPlatform(SiPlatformAssignIn req) {
        List<RealmPlatformGrantPO> exists = super.lambdaQuery()
                .eq(RealmPlatformGrantPO::getRealmId, req.getRealmId())
                .in(RealmPlatformGrantPO::getPlatformId, req.getPlatIds())
                .list();
        if (null != exists) {
            for (RealmPlatformGrantPO exist : exists) {
                req.getPlatIds().remove(exist.getPlatformId());
            }
        }

        if (!req.getPlatIds().isEmpty()) {
            List<RealmPlatformGrantPO> newGrant = new ArrayList<>();
            for (String platId : req.getPlatIds()) {
                RealmPlatformGrantPO grantPo = new RealmPlatformGrantPO();
                grantPo.setId(IdGenUtil.buildGrantPlatformId());
                grantPo.setRealmId(req.getRealmId());
                grantPo.setPlatformId(platId);
                grantPo.setGrantType(req.getGrantType());
                grantPo.setUpdateTime(LocalDateTime.now());
                grantPo.setCreateTime(LocalDateTime.now());
                newGrant.add(grantPo);
            }
            super.saveBatch(newGrant);
        }
    }

    @Override
    public void removePlatform(SiPlatformAssignIn req) {
        super.lambdaUpdate()
                .eq(RealmPlatformGrantPO::getRealmId, req.getRealmId())
                .in(RealmPlatformGrantPO::getPlatformId, req.getPlatIds())
                .remove();
    }

    @Override
    public int realmGrantToPlatform(SiRealmGrantPlatformIn req) {
        RealmPlatformGrantPO result = super.lambdaQuery()
                .eq(RealmPlatformGrantPO::getRealmId, req.getRealmId())
                .eq(RealmPlatformGrantPO::getPlatformId, req.getPlatformId())
                .one();
        return null == result ? -1 : result.getGrantType() == null ? 1 : result.getGrantType();
    }

    @Override
    public List<RealmPlatformGrantEntity> queryByRealmId(String realmId, Integer grantType) {
        return uniUserSingleAssembler.fromRealmPlatformGrant(super.lambdaQuery()
                .eq(RealmPlatformGrantPO::getRealmId, realmId)
                .eq(null != grantType, RealmPlatformGrantPO::getGrantType, grantType)
                .list());
    }

    @Override
    public List<RealmPlatformGrantEntity> queryByPlatformId(String platformId, Integer grantType) {
        return uniUserSingleAssembler.fromRealmPlatformGrant(super.lambdaQuery()
                .eq(RealmPlatformGrantPO::getPlatformId, platformId)
                .eq(null != grantType, RealmPlatformGrantPO::getGrantType, grantType)
                .list());
    }
}
